/**
 * Title:        代理数据同步
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;



public class RequestSequenceCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("RequestSequenceCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        if(recordSet.getDBType().equals("oracle")){

            //currentoperator
            int curoptCurVal = -1;
            int curoptMaxId = -1;
            recordSet.executeSql(" select t_wf_curopt_id.nextval from dual ");
            if(recordSet.next()){
                curoptCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(id) from workflow_currentoperator ");
            if(recordSet.next()){
                curoptMaxId = recordSet.getInt(1);
            }
            if(curoptCurVal < curoptMaxId){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","操作者表中最大的ID(" + curoptMaxId + ")大于对应的SEQ中的当前值(" + curoptCurVal + ")，会导致插入错误。");
                resultMap.put("buttonname","数据同步");
                resultMap.put("seqid","t_wf_curopt_id");
                resultList.add(resultMap);
            }

            //requestLog
            int requestLogCurVal = -1;
            int requestLogMaxId = -1;
            recordSet.executeSql(" select workflow_requestlog_id.nextval from dual ");
            if(recordSet.next()){
                requestLogCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(logid) from workflow_requestlog ");
            if(recordSet.next()){
                requestLogMaxId = recordSet.getInt(1);
            }
            if(requestLogCurVal < requestLogMaxId){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","流转意见表中最大的ID(" + requestLogMaxId + ")大于对应的SEQ中的当前值(" + requestLogCurVal + ")，会导致插入错误。");
                resultMap.put("buttonname","数据同步");
                resultMap.put("seqid","workflow_requestlog_id");
                resultList.add(resultMap);
            }
            if(resultList.size() <= 0){
                result = resultOk;
                resultList.add("数据检测正常。");
            }
            //requestId
            int requestidCurVal = -1;
            int requestidMaxId = -1;
            recordSet.executeSql(" select requestid from workflow_requestsequence ");
            if(recordSet.next()){
                requestidCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(logid) from workflow_requestlog ");
            if(recordSet.next()){
                requestidMaxId = recordSet.getInt(1);
            }
            if(requestidCurVal < requestidMaxId || requestidCurVal == -1){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","流程中最大的ID(" + requestidMaxId + ")大于对应的SEQ中的当前值(" + requestidCurVal + ")，会导致插入错误。");
                resultMap.put("buttonname","数据同步");
                resultMap.put("seqid","workflow_requestlog_id");
                resultList.add(resultMap);
            }
            if(resultList.size() <= 0){
                result = resultOk;
                resultList.add("数据检测正常。");
            }
        }
        
        (new BaseBean()).writeLog("RequestSequenceCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        setRecordSet();
        (new BaseBean()).writeLog("RequestSequenceCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        if(recordSet.getDBType().equals("oracle")){

            //currentoperator
            int curoptCurVal = -1;
            int curoptMaxId = -1;
            recordSet.executeSql(" select t_wf_curopt_id.nextval from dual ");
            if(recordSet.next()){
                curoptCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(id) from workflow_currentoperator ");
            if(recordSet.next()){
                curoptMaxId = recordSet.getInt(1);
            }
            if(curoptCurVal != -1 && curoptCurVal < curoptMaxId){
                for(int i = curoptCurVal; i < curoptMaxId;i ++ ){
                    recordSet.executeSql(" select t_wf_curopt_id.nextval from dual ");
                }
            }

            //requestLog
            int requestLogCurVal = -1;
            int requestLogMaxId = -1;
            recordSet.executeSql(" select workflow_requestlog_id.nextval from dual ");
            if(recordSet.next()){
                requestLogCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(logid) from workflow_requestlog ");
            if(recordSet.next()){
                requestLogMaxId = recordSet.getInt(1);
            }
            if(curoptCurVal != -1 && requestLogCurVal < requestLogMaxId){
                for(int i = requestLogCurVal; i < requestLogMaxId;i ++ ){
                    recordSet.executeSql(" select workflow_requestlog_id.nextval from dual ");
                }
            }

            //requestId
            int requestidCurVal = -1;
            int requestidMaxId = -1;
            recordSet.executeSql(" select requestid from workflow_requestsequence  ");
            int cnt = recordSet.getCounts();
            if(recordSet.next()){
                requestidCurVal = recordSet.getInt(1);
            }
            
            recordSet.executeSql(" select max(logid) from workflow_requestlog ");
            if(recordSet.next()){
                requestidMaxId = recordSet.getInt(1);
            }
            if(cnt == 0){
                if(requestidMaxId <= 0){
                    requestidMaxId = 1;
                }
                recordSet.executeSql(" insert into workflow_requestsequence values(" + requestidMaxId + ") ");
            }else if(requestidCurVal < requestidMaxId){
                recordSet.executeSql(" update workflow_requestsequence set requestid = " + requestidMaxId);
            }
        }
        needReload = "1";
        return checkData(new HashMap());
        
        
    }

    @Override
    public String getClassName() {
        return "检测SEQ当前值";
    }

    @Override
    public String getClassDesc() {
        return "检测流转意见表和操作者表，SEQ是否大于该表最大的id";
    }

}



